<template>
	<view v-if="goods_info.goods_name" class="goods-detail-container">
		<!-- 轮播图区域 -->
		<swiper :indicator-dots=" true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in goods_info.pics" :key="index">
				<image :src="item.pics_big" mode="" @click="preview(index)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="goods-info-box">
			<!-- 商品价格 -->
			<view class="price">
				￥{{goods_info.goods_price | tofixed}}
			</view>
			<!-- 商品信息主体区域 -->
			<view class="zhuti">
				<view class="text-name">
					{{goods_info.goods_name}}
				</view>
				<view class="shou">
					<uni-icons type="star" size="25" color="#898989"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="yunfei">
				快递：免运费
			</view>
		</view>

		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		<uni-goods-nav fill="true " @click="onClick" :options="options" @buttonClick="buttonClick" />
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'

	export default {
		computed: {
			...mapGetters('m_cart', ['total'])
		},
		watch: {
			total: {
				// handler属性用来定义侦听器的处理函数
				handler(newVal) {
					const findResult = this.options.find(x => x.text === '购物车')
					if (findResult) {
						findResult.info = newVal
					}
					// 购物车的tabBar数字也要更新
				},
				// immediate用来声明此侦听器是否再页面初次加载完毕后立即调用
				immediate: true
			}
		},
		data() {
			return {
				//商品详情对象
				goods_info: {},
				// 左侧按钮组的配置对象
				options: [{
					icon: 'shop',
					text: '店铺'
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				//右侧按钮组的配置对象
				buttonGroup: [{
					text: '加入购物车',
					backgroundColor: '#ff0000',
					color: '#fff'
				}, {
					text: '立即购买',
					backgroundColor: '#ffa200',
					color: '#fff'
				}]
			}
		},
		filters: {
			tofixed(num) {
				return Number(num).toFixed(2)
			}
		},
		methods: {
			...mapMutations('m_cart', ['addToCart', 'setBadge']),
			async getGoodsDetail(goods_id) {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id
				})
				// console.log(res);
				if (res.meta.status !== 200) return uni.$showMsg()
				// 使用字符串的replace方法，为img标签添加行内的style样式，从而解决图片底部空白间隙的问题
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
					'<img style="display:block;"').replace(/webp/g, 'jpg')
				this.goods_info = res.message
			},
			//轮播图预览
			preview(index) {
				//调用uni.previewImage方法预览图片
				uni.previewImage({
					//预览时，默认显示图标的索引
					current: index,
					// 所有图片url地址的数组
					urls: this.goods_info.pics.map(x => x.pics_big)
				})
			},
			onClick(e) {
				// console.log(e.content.text);
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			buttonClick(e) {
				// console.log(e);
				if (e.content.text === '加入购物车') {
					//每个商品的信息对象，都包含如下六个属性
					//{goods_id,goods_name,goods_price,goods_count,goods_small_logo,goods_state}
					const goods = {
						goods_id: this.goods_info.goods_id,
						goods_name: this.goods_info.goods_name,
						goods_price: this.goods_info.goods_price,
						goods_count: 1,
						goods_small_logo: this.goods_info.goods_small_logo,
						goods_state: true
					}
					this.addToCart(goods)
				}
			}
		},
		onLoad(options) {
			const goods_id = options.goods_id
			// console.log(options.goods_id)
			//调用请求商品详情数据的方法
			this.getGoodsDetail(goods_id)
		}
	}
</script>

<style lang="scss">
	.goods-detail-container {
		padding-bottom: 50px;
	}

	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.goods-info-box {
		margin: 5px 10px 20px 5px;
		display: flex;
		flex-wrap: wrap;

		.price {
			width: 100%;
			height: 50px;
			font-size: 18px;
			color: #87031d;
			line-height: 50px;
		}

		.zhuti {
			display: flex;
			width: 100%;

			.text-name {
				width: 85%;
				font-size: 14px;
				border-right: 1rpx #898989 solid;
				padding-right: 18rpx;
			}

			.shou {
				width: 15%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				font-size: 12px;
				color: #898989;
			}
		}

		.yunfei {
			font-size: 12px;
			color: #898989;
			margin-top: 10px;
		}
	}

	uni-goods-nav {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
</style>
